#include "common.h"
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>

/* https://stackoverflow.com/questions/3756323/how-to-get-the-current-time-in-milliseconds-from-c-in-linux */
long long current_timestamp() {
    struct timeval te;
    gettimeofday(&te, 0); // get current time
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
    // printf("milliseconds: %lld\n", milliseconds);
    return milliseconds;
}

char* cp_str(char* src) {
	size_t len = strnlen(src, MAXLINE) + 1;
	char* dest = malloc(sizeof(char) * len);
	strncpy(dest, src, len);
	return dest;
}

char *substr(const char *inpStr, char *outStr, size_t startPos, size_t strLen) {
  strncpy(outStr, inpStr + startPos, strLen);
  outStr[strLen] = '\0';
  return outStr;
}

char *substr_with_alloc(const char* inpStr, size_t startPos, size_t strLen) {
	size_t len = strLen + 1;
	char* dest = malloc(sizeof(char) * len);
	substr(inpStr, dest, startPos, strLen);
	return dest;
}
